package com.heima.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.admin.mapper.ChannelMapper;
import com.heima.admin.service.ChannelService;
import com.heima.model.admin.dtos.ChannelDTO;
import com.heima.model.admin.pojos.AdChannel;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @Program: day57-leadnews
 * @Description:
 * @Data: 2022/7/25 21:28
 **/
@Service
public class ChannelServiceImpl extends ServiceImpl<ChannelMapper, AdChannel> implements ChannelService {

    @Override
    public PageResponseResult pageQuery(ChannelDTO dto) {
        //1: 参数校验
        dto.checkParam();
        //2: 分页查询
        Page<AdChannel> pageReq = new Page<>(dto.getPage(), dto.getSize());
        //2.1: 封装分页查询条件
        LambdaQueryWrapper<AdChannel> queryWrapper = Wrappers.lambdaQuery();
        // 判断数据是否存在
        if (StringUtils.isNotBlank(dto.getName())){ // true 代表name不为空
            queryWrapper.like(AdChannel::getName,dto.getName());
        }
        if (dto.getStatus() != null){
            queryWrapper.eq(AdChannel::getStatus,dto.getStatus());
        }
        // 按照 ord 升序排列
        queryWrapper.orderByAsc(AdChannel::getOrd);
        //2.2: 其他查询条件
        //2.3: 执行查询操作
        // 参数1：分页条件   参数2：查询条件
        IPage<AdChannel> pageResult = this.page(pageReq, queryWrapper);
        //3: 封装 pageResponseResult 结果返回
        return new PageResponseResult(dto.getPage(),dto.getSize(),pageResult.getTotal(),pageResult.getRecords());
    }

    @Override
    public ResponseResult insert(AdChannel adchannel) {
        // 1:参数校验
        String name = adchannel.getName();
        // 判断频道名称不能为空
        if (StringUtils.isBlank(name)){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"频道名称不能为空");
        }
        // 判断频道名称不能大于10
        if (name.length() > 10){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"频道名称不能大于10个字符");
        }
        // 判断频道名称不能重复
        int count = this.count(Wrappers.<AdChannel>lambdaQuery().eq(AdChannel::getName,name));
        if (count > 0){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"添加失败，通道名称已存在!");
        }
        // 2:将频道保存到数据库
        adchannel.setCreatedTime(new Date());
        this.save(adchannel);
        // 3:返回封装结果
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult update(AdChannel adChannel) {
        // 1:参数校验
        // 判断数据是否存在
        if (adChannel.getId() == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"所修改的数据不存在！");
        }
        // 查询频道id
        // 根据id查询频道信息
        AdChannel oldChannel = this.getById(adChannel.getId());
        if (oldChannel == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST,"频道不存在！");
        }
        // 判断参数name是否为空，如果不为空，判断与原来的频道名称是否一致
        String name = adChannel.getName();
        if (StringUtils.isNotBlank(name) && !name.equals(oldChannel.getName())){
            // 判断频道名称不能大于10
            if (name.length() > 10){
                return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"频道名称不能大于10个字符");
            }
            // 判断频道名称不能重复
            int count = this.count(Wrappers.<AdChannel>lambdaQuery().eq(AdChannel::getName,name));
            if (count > 0){
                return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"添加失败，通道名称已存在!");
            }
        }
        // 2:修改频道
        this.updateById(adChannel);
        // 3:响应结果
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult deleteById(Integer id) {
        // 1:参数校验
        // 通过id判断数据是否存在
        if (id == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        // 通过id，判读频道是否存在
        AdChannel adChannel = this.getById(id);
        if (adChannel == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST,"所删除的频道不存在！");
        }
        // 启用状态下的频道不能删除
        if (adChannel.getStatus()){ // 默认状态为true：启动状态
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE,"频道处于启动状态，无法删除！");
        }
        this.removeById(id);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
